import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { DictService } from './dict.service';
import { QueryDictDto } from './dto/query.dict.dto';
import { CreateDictDto } from './dto/create-dict.dto';
import { UpdateDictDto } from './dto/update-dict.dto';
import { CreateDictDataDto } from './dto/create-dict-data.dto';
import { UpdateDictDataDto } from './dto/update-dict-data.dto';

@ApiTags('admin-字典管理')
@Controller('dict')
export class DictController {
  constructor(private readonly dictService: DictService) {}

  @ApiOperation({ summary: '获取字典列表' })
  @Get('list')
  async getDictList(@Query() query: QueryDictDto) {
    return await this.dictService.getDictList(query);
  }

  @ApiOperation({ summary: '根据字典编码获取字典数据' })
  @Get('data/:dictCode')
  async getDictDataByCode(
    @Param('dictCode') dictCode: string,
    @Query('pageNum') pageNum?: number,
    @Query('pageSize') pageSize?: number,
    @Query('label') label?: string,
    @Query('value') value?: string,
  ) {
    return await this.dictService.getDictDataByCode(
      dictCode,
      pageNum,
      pageSize,
      label,
      value,
    );
  }

  @ApiOperation({ summary: '创建字典' })
  @Post()
  async createDict(@Body() createDictDto: CreateDictDto) {
    return await this.dictService.createDict(createDictDto);
  }

  @ApiOperation({ summary: '更新字典' })
  @Put(':id')
  async updateDict(
    @Param('id') id: number,
    @Body() updateDictDto: UpdateDictDto,
  ) {
    return await this.dictService.updateDict(id, updateDictDto);
  }

  @ApiOperation({ summary: '删除字典' })
  @Delete(':id')
  async deleteDict(@Param('id') id: number) {
    return await this.dictService.deleteDict(id);
  }

  @ApiOperation({ summary: '创建字典数据项' })
  @Post('data')
  async createDictData(@Body() createDictDataDto: CreateDictDataDto) {
    return await this.dictService.createDictData(createDictDataDto);
  }

  @ApiOperation({ summary: '更新字典数据项' })
  @Put('data/:id')
  async updateDictData(
    @Param('id') id: number,
    @Body() updateDictDataDto: UpdateDictDataDto,
  ) {
    return await this.dictService.updateDictData(id, updateDictDataDto);
  }

  @ApiOperation({ summary: '删除字典数据项' })
  @Delete('data/:id')
  async deleteDictData(@Param('id') id: number) {
    return await this.dictService.deleteDictData(id);
  }

  @ApiOperation({ summary: '刷新字典缓存' })
  @Get('refresh-cache')
  async refreshCache(@Query('dictCode') dictCode?: string) {
    return await this.dictService.refreshCache(dictCode);
  }

  @ApiOperation({ summary: '获取多个字典及其数据' })
  @Get('with-data')
  async getAllDictWithData(@Query('dictCodes') dictCodes?: string) {
    // 如果提供了dictCodes参数，则将其转换为数组
    const dictCodeArray = dictCodes ? dictCodes.split(',') : undefined;
    return await this.dictService.getAllDictWithData(dictCodeArray);
  }

  @ApiOperation({ summary: '根据ID获取字典数据项' })
  @Get('data/detail/:id')
  async getDictDataById(@Param('id') id: number) {
    return await this.dictService.getDictDataById(id);
  }
}
